package dmir.reaction.publico.parsers;

import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


	/*
	 * SAX parser to process a GraphML file and generate an input file for CFinder  
	 */


public class GraphMLParser extends DefaultHandler {
	
	boolean getData = false;
	boolean inEdge = false;
	boolean inDataWeigth = false;
	String data;
	String id1;
	String id2;
	String weight;
	
	HashMap<Integer,String> ids_entities = new HashMap<Integer,String>(); 
	
	public GraphMLParser(HashMap<Integer, String> ids_entities) {
		this.ids_entities = ids_entities;
	}

	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		
		if (qName.equalsIgnoreCase("edge")) {
			getData = true;	
			inEdge = true;			
			id1 = attributes.getValue(1);
			id2 = attributes.getValue(2);
		}
		
		if (qName.equalsIgnoreCase("data")) {
			if (attributes.getValue(0).equalsIgnoreCase("d5")) {
				inDataWeigth = true;
				getData = true;
			}			
		}
	}

	public void characters(char ch[], int start, int length) throws SAXException {
		if (getData)
			this.data = new String(ch, start, length);
	}
	
	public void endElement(String uri, String localName, String qName) throws SAXException {		
		
		if (qName.equalsIgnoreCase("data") && inDataWeigth) {
			 weight = this.data;			 
			 inDataWeigth = false;
			 getData = false;
		}
		
		if (qName.equalsIgnoreCase("edge")) {
			System.out.println(id1 + '\t' + id2 + '\t' + weight);
			//System.out.println(ids_entities.get(Integer.parseInt(id1)) + '\t' + ids_entities.get(Integer.parseInt(id2)) + '\t' + weight);
			inEdge = false;
			getData = false;
		}	
	}
}







